package cn.js.ccit.controller;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.js.ccit.pojo.Meeting;
import cn.js.ccit.pojo.MeetingCommand;
import cn.js.ccit.service.MeetingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
@RequestMapping("/meetingController")
public class MeetingController {
	private String viewPage;
	
	@Autowired
	private MeetingService meetingService;
	
	public void setViewPage(String viewPage){
		this.viewPage=viewPage;
	}

	@RequestMapping("/applyMetting")
	public String applyMeeting(HttpServletRequest request,
							   HttpServletResponse response, Model model) throws Exception {
	
		String title=request.getParameter("title");
		String place=request.getParameter("place");
		String host=request.getParameter("host");
		String particiPant=request.getParameter("particiPant");
		String departMent=request.getParameter("departMent");
		String meetingLevel=request.getParameter("meetingLevel");
		String budget=request.getParameter("budget");
		String startDate=request.getParameter("startDate");
		String startTime=request.getParameter("startTime");
		String endDate=request.getParameter("endDate");
		String endTime=request.getParameter("endTime");
		//String stautsStr=request.getParameter("stauts");
		//String approvingPerson=request.getParameter("title");
	//	String approvingDate=request.getParameter("title");
		String topic=request.getParameter("topic");
		String notes=request.getParameter("notes");
				
		System.out.println("title="+title);
	
		Meeting meeting=new Meeting();
		meeting.setTitle(title);
		meeting.setPlace(place);
		meeting.setHost(host);
		meeting.setParticiPant(particiPant);
		meeting.setDepartMent(departMent);
		meeting.setMeetingLevel(meetingLevel);	
		meeting.setBudget(budget);
		meeting.setStartDate(startDate);
		meeting.setStartTime(startTime);
		meeting.setEndDate(endDate);
		meeting.setEndTime(endTime);
		meeting.setStauts(0);
		meeting.setApprovingPerson("");
		meeting.setApprovingDate("");
		meeting.setTopic(topic);
		meeting.setNotes(notes);
		
		
		meetingService.addMeeting(meeting);
		model.addAttribute("meeting",meeting);
		
		return "addMeetingSuccess";
	}

	@RequestMapping("/listMeeting")
	// 获取会议申请信息
	public String listMeeting(HttpServletRequest request,
									HttpServletResponse response,Model model) throws Exception {
		System.out.println("list   meeting.....");

		List meetingList = meetingService.getAllMeeting();
		System.out.println("meetingList.size=" + meetingList.size());
		model.addAttribute("meetingList",meetingList);
		return "showMeeting";
	}

	@RequestMapping("/preApproval")
	//审批会议预处理
	public String preApproval(HttpServletRequest request,
									HttpServletResponse response,Model model) throws Exception {
		System.out.println("preApproval   meeting.....");
		// 获取id
		String idStr = request.getParameter("id");
		if (idStr == null) {
			idStr = "0";
		}

		int id=Integer.parseInt(idStr);
		System.out.println("id="+id);

		Meeting meeting=meetingService.getMeetingById(id);
		model.addAttribute("meeting",meeting);
		return "approvalMeeting";
	}

	@RequestMapping("/approvalMeeting")
	//审批会议
	public String approval(HttpServletRequest request,
								 HttpServletResponse response) throws Exception {
		System.out.println("approval   meeting.....");
		// 获取id
		String idStr = request.getParameter("id");
		if (idStr == null) {
			idStr = "0";
		}

		int id=Integer.parseInt(idStr);
		System.out.println("id="+id);


		//	status=?,approvingPerson=?,approvingDate=?  id

		String stautsStr=request.getParameter("status");

		int stauts=Integer.parseInt(stautsStr);

		//设定审批人
		String approvingPerson="院办主任";

		//审批日期
		Date date=new Date();
		SimpleDateFormat simple=new SimpleDateFormat("yyyy-MM-dd");
		String approvingDate=simple.format(date);

		Meeting meeting=new Meeting();
		meeting.setId(id);
		meeting.setStauts(stauts);
		meeting.setApprovingPerson(approvingPerson);
		meeting.setApprovingDate(approvingDate);

		boolean result=meetingService.approvalMeeting(meeting);
		return "approvalSuccess";
	}


	@RequestMapping("/queryMeeting")
	public String queryMeeting(HttpServletRequest request, MeetingCommand meetingCommand,Model model){

		System.out.println("queryMeeting .....");
		String title=meetingCommand.getTitle();
		String departMent=meetingCommand.getDepartMent();
		if(title==null) title="";
		if(departMent==null) departMent="";

		System.out.println("title"+title);
		System.out.println("depatment="+departMent);

		List meetingList=meetingService.queryMeeting(title, departMent);
		model.addAttribute("meetingList",meetingList);
		return "showMeeting";
	}

	@RequestMapping("/queryMeetingBymyBatis")
	public String queryMeetingBymyBatis(HttpServletRequest request, MeetingCommand meetingCommand,Model model){

		System.out.println("queryMeeting .....");
		String title=meetingCommand.getTitle();
		String departMent=meetingCommand.getDepartMent();
		if(title==null) title="";
		if(departMent==null) departMent="";

		System.out.println("title"+title);
		System.out.println("depatment="+departMent);

		List meetingList=meetingService.queryMeetingByMyBatis(title,departMent);
		model.addAttribute("meetingList",meetingList);
		return "showMeeting";
	}





}
